<template>
  <div>
    <div>
      <van-nav-bar title="标题" left-text="返回" left-arrow @click-left="back()">
      </van-nav-bar>
    </div>
    <div>
      <van-address-edit
        :area-list="areaList"
        show-postal
        show-delete
        show-set-default
        @save="addAddress"
        :address-info="userInfoList"
      />
    </div>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
export default {
  name: "Editaddress",
  data() {
    return {
      areaList: areaList,
      //用户地址信息
      userInfoList: {},
      appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
      tokenString: this.$cookies.get("TOKEN"),
      aids: "",
      b: "",
    };
  },
  created() {
    let a = this.$route.query.a;
    let b = this.$route.query.b;
    this.b = b;
    this.aids = a;
    this.findEditAddress(a);
  },
  methods: {
     back() {
      this.$router.go(-1);
    },
    //查询要编辑的地址
    findEditAddress(aid) {
      this.axios({
        method: "get",
        url: "/findAddressByAid",
        params: {
          appkey: this.appkey,
          tokenString: this.tokenString,
          aid,
        },
      }).then((res) => {
        this.userInfoList = res.data.result[0];
        console.log("这是要编辑的地址==>", res);
      });
    },
    addAddress(e) {
      this.userInfoList = e;
      //新增地址参数
      let str = `appkey=${this.appkey}&tokenString=${this.tokenString}&name=${
        this.userInfoList.name
      }&tel=${this.userInfoList.tel}&province=${
        this.userInfoList.province
      }&city=${this.userInfoList.city}&county=${
        this.userInfoList.county
      }&addressDetail=${this.userInfoList.addressDetail}&areaCode=${
        this.userInfoList.areaCode
      }&postalCode=${this.userInfoList.postalCode}&isDefault=${Number(
        this.userInfoList.isDefault
      )}`;
      //编辑地址参数
      let str1 = `appkey=${this.appkey}&tokenString=${this.tokenString}&name=${
        this.userInfoList.name
      }&aid=${this.aids}&tel=${this.userInfoList.tel}&province=${
        this.userInfoList.province
      }&city=${this.userInfoList.city}&county=${
        this.userInfoList.county
      }&addressDetail=${this.userInfoList.addressDetail}&areaCode=${
        this.userInfoList.areaCode
      }&postalCode=${this.userInfoList.postalCode}&isDefault=${Number(
        this.userInfoList.isDefault
      )}`;
      let url = "";
      if (this.b == true) {
        url = "/editAddress";
      } else {
        url = "/addAddress";
      }
      this.axios({
        method: "post",
        url,
        data: url == "/editAddress" ? str1 : str,
      }).then((res) => {
        console.log(res);
      });
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.van-button--danger {
  background-color: #0c34ba;
  border: 1px solid #0c34ba;
}
</style>